package org.darkforest.common.mail;


import org.darkforest.common.logger.Logger;
import org.darkforest.common.util.SystemConstant;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * MailUtil
 * Created by Justin on 15/3/14.
 */
public class MailUtil {

    public static String HOST = "smtp.126.com";
    public static String AUTH = "false";
    public static String USER_NAME = "llbb909@126.com";
    public static String PASSWORD = "";
    public static String SENDER = "17600603689@163.com";
    public static String DEBUG = "true";
    public static String PORT = "25";
    public static String RECEIVER = "xxxxxx@qq.com";
    private static MailUtil instance;

    private MailSender mailSender;

    private MailUtil() {
        mailSender = new MailSender();
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setUsername(SystemConstant.USERNAME.trim());
        javaMailSender.setPassword(SystemConstant.PASSWORD.trim());
        javaMailSender.setPort(Integer.valueOf(SystemConstant.PORT.trim()));
        javaMailSender.setHost(SystemConstant.HOST.trim());
        javaMailSender.setProtocol(JavaMailSenderImpl.DEFAULT_PROTOCOL);

        SENDER = SystemConstant.SENDER.trim();

//        javaMailSender.setUsername(USER_NAME);
//        javaMailSender.setPassword(PASSWORD);
//        javaMailSender.setPort(Integer.valueOf(PORT));
//        javaMailSender.setHost(HOST);
//        javaMailSender.setProtocol(JavaMailSenderImpl.DEFAULT_PROTOCOL);
        javaMailSender.setJavaMailProperties(new Properties() {
            {
                setProperty("mail.smtp.auth", AUTH);
            }
        });
        mailSender.setMailSender(javaMailSender);
    }

    private static MailUtil getInstance() {
        if (instance == null) {
            instance = new MailUtil();
        }
        return instance;
    }

    public static void sendMail(String subject, String content, String sender, String... receivers) {
        getInstance().mailSender.sendAsynchronousMail(subject, content, sender, receivers);
    }

    public static void sendMail(String subject, String content, String[] receivers,
                                String attachmentFilename[], File attach[], boolean attachAsMIME) {
        getInstance().mailSender.send(subject, content, receivers, SENDER, null, null,
                attachmentFilename, attach, attachAsMIME);
    }
    public static void sendMail(String subject, String content, String sender, String[] receivers,String[] cc, String[] bcc,
                                String attachmentFilename[], File attach[], boolean attachAsMIME) {
        getInstance().mailSender.send(subject, content, receivers, sender, cc, bcc,
                attachmentFilename, attach, attachAsMIME);
    }

    public static String sendMailBySSL(String subject, String content, String sender, String... receivers) {
       return  MailSSLSender.sendMail(subject, content, sender, receivers);
    }

    public static String sendMailBySSL(String subject, String content, String sender, String[] receivers,
                                       String[] cc, String[] bcc, Map<String,String> imageMap, List<String> filelist) {

        String  retVal = MailSSLSender.sendMail(subject, content, sender, receivers, cc, bcc, imageMap, filelist);

        return  retVal;
    }

    public static void main(String[] args){
        String object="测试";
        String content="<img src='cid:cs00001'>";
        String[] receivers={"243569750@qq.com"};
//        String sender="llbb909@126.com";
        String[] attachmentFilename = {"cs00001"};
        File[] attach = {new File("d://windows.jpg")};
        MailUtil.sendMail(object, content,receivers,attachmentFilename,attach,false);
    }
}
